package cn.vcjf.washcar.common.util.weixin;

import cn.vcjf.washcar.common.util.httpUtils.HttpClientUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

/**
 * 微信授权类
 *
 * @author jauyang
 */
public class WxPayAuthApi {

	public static String APP_ID = "wxd67eb8b4e533fcec";

	public static String APP_SECRET = "eda6cc451c69ffda5ed1b47a102a4f09";

	private static String ACCESS_URL = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=APPSECRET&code=CODE&grant_type=authorization_code";

	public static String getRedirectUrl(String redirectUrl){
		return "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + APP_ID + "&redirect_uri=" + redirectUrl + "&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
	}
	/**
	 *
	 */
	public void doGetCode(HttpServletRequest request, HttpServletResponse response, String appId, String appSecret, String redirectUrl) throws IOException {
		if (request.getParameter("code") == null) {
			redirectUrl = URLEncoder.encode(redirectUrl, "UTF-8");
			response.sendRedirect("https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appId + "&redirect_uri=" + redirectUrl + "&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect");
		} else {
			response.getOutputStream().print(getOpenId(request, response, appId, appSecret));
		}
	}
	/**
	 *
	 */
	public static String getCode(HttpServletRequest request, HttpServletResponse response, String appId, String appSecret, String redirectUrl) throws IOException {
		if (request.getParameter("code") == null) {
			System.out.println("========================get-code==================");
			redirectUrl = URLEncoder.encode(redirectUrl, "UTF-8");
			response.sendRedirect("https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appId + "&redirect_uri=" + redirectUrl + "&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect");
		} else {
			return getOpenId(request, response, appId, appSecret);
		}
		return null;
	}

	/**
	 * 获取微信授权的openid
	 */
	public static String getOpenId(HttpServletRequest request, HttpServletResponse response, String appId, String appSecret) {

		try {
			System.out.println("========================get-OpenId==================");
			request.setCharacterEncoding("UTF-8");
			response.setCharacterEncoding("UTF-8");
			String code = request.getParameter("code");
			if (StringUtils.isEmpty(code)) {
				return null;
			}
			String openId = "";
			String requestUrlString = ACCESS_URL.replace("APPID", appId).replace("APPSECRET", appSecret).replace("CODE", code);
			HttpResponse result = HttpClientUtils.exePOSTMethod((Map<String, String>) new HashMap<String, String>(), requestUrlString);
			if (HttpClientUtils.isHttpResponseOk(result)) {
				HttpEntity entity = result.getEntity();
				String message = EntityUtils.toString(entity, "utf-8");

				JSONObject resultObject = JSON.parseObject(message);
				openId = resultObject.getString("openid");
				System.out.println("openId = "+openId);
				//oYCFB0opzyfpg-BmTVTCcZchAE2k
			}
			return StringUtils.isBlank(openId) ? "" : openId;
		} catch (JSONException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;

	}
}
